package com.cskaoyan.wordmemorize.controller.admin;

import cn.dev33.satoken.stp.StpUtil;
import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeletePermissionOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PermissionDTO;
import com.cskaoyan.wordmemorize.dto.admin.RolePermissionDTO;
import com.cskaoyan.wordmemorize.request.PermissionCommand;
import com.cskaoyan.wordmemorize.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin")
public class PermissionManage {
    @Autowired
    RolePermissionService rolePermissionService;

    @SaAdminCheckPermission("admin:permission:search")
    @GetMapping("permission/list")
    public Result<List<PermissionDTO>> getPermissionList() {

        List<PermissionDTO> permissions = rolePermissionService.getPermissions();
        return Result.ok(permissions);
    }

    @SaAdminCheckPermission({"admin:permission:add", "admin:permission:add:sub"})
    @PostMapping("permission")
    public Result savePermissionTree(@RequestBody PermissionCommand permissionCommand) {
        rolePermissionService.savePermissions(permissionCommand);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:permission:delete")
    @DeleteMapping("permission/{permissionId}")
    public Result deletePermission(@PathVariable("permissionId") Long permissionId) {
        DeletePermissionOperateDTO deletePermissionOperateDTO = rolePermissionService.deletePermission(permissionId);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:permission:update")
    @PutMapping ("permission")
    public Result updatePermission(@RequestBody PermissionCommand permissionCommand) {
        rolePermissionService.updatePermission(permissionCommand);
        return Result.ok();
    }


}
